﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmUsuario
{
    public partial class Listado_Usuario : Form
    {
        private DataTable dt;
        private Modelo.FormActions.Actions action;

        public Listado_Usuario(Modelo.FormActions.Actions runAction)
        {
            action = runAction;
            InitializeComponent();
            FillData();
        }

        void FillData()
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da;
                dt = new DataTable();
                switch (action)
                {
                    case Modelo.FormActions.Actions.Baja:
                        da = new SqlDataAdapter(Modelo.SqlQueries.getEnabledUsers, con);
                        da.Fill(dt);
                        break;
                    case Modelo.FormActions.Actions.Modificacion:
                        da = new SqlDataAdapter(Modelo.SqlQueries.getUsers, con);
                        da.Fill(dt);
                        break;
                }
                dataGridView1.DataSource = dt;
            }

        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int columnIndex = e.ColumnIndex;
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            Login.Login.mainForm.openForm(new AbmUsuario(action, Convert.ToInt32(row.Cells["Id"].Value)));
        }

        private void buscar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getUsersFiltered(busqueda_tb.Text), con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }

        private void limpiar_btn_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getUsers, con);
                dt.Dispose();
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
        }
    }
}
